home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / htmlparser / nsHTMLTokens.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  14KB  |  458 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /**
  39.  * MODULE NOTES:
  40.  * @update  gess 4/1/98
  41.  *
  42.  * This file contains the declarations for all the HTML specific token types that
  43.  * our DTD's understand. In fact, the same set of token types are used for XML.
  44.  * Currently we have tokens for text, comments, start and end tags, entities,
  45.  * attributes, style, script and skipped content. Whitespace and newlines also
  46.  * have their own token types, but don't count on them to stay forever.
  47.  *
  48.  * If you're looking for the html tags, they're in a file called nsHTMLTag.h/cpp.
  49.  *
  50.  * Most of the token types have a similar API. They have methods to get the type
  51.  * of token (GetTokenType); those that represent HTML tags also have a method to
  52.  * get type tag type (GetTypeID). In addition, most have a method that causes the
  53.  * token to help in the parsing process called (Consume). We've also thrown in a
  54.  * few standard debugging methods as well.
  55.  */
  56.  
  57. #ifndef HTMLTOKENS_H
  58. #define HTMLTOKENS_H
  59.  
  60. #include "nsToken.h"
  61. #include "nsHTMLTags.h"
  62. #include "nsString.h"
  63. #include "nsScannerString.h"
  64.  
  65. class nsScanner;
  66.  
  67.   /*******************************************************************
  68.    * This enum defines the set of token types that we currently support.
  69.    *******************************************************************/
  70.  
  71. enum eHTMLTokenTypes {
  72.   eToken_unknown=0,
  73.   eToken_start=1,      eToken_end,          eToken_comment,         eToken_entity,
  74.   eToken_whitespace,   eToken_newline,      eToken_text,            eToken_attribute,
  75.   eToken_instruction,  eToken_cdatasection, eToken_doctypeDecl,     eToken_markupDecl,
  76.   eToken_last //make sure this stays the last token...
  77. };
  78.  
  79. enum eHTMLCategory {
  80.   eHTMLCategory_unknown=0,
  81.   eHTMLCategory_inline,
  82.   eHTMLCategory_block,
  83.   eHTMLCategory_blockAndInline,
  84.   eHTMLCategory_list,
  85.   eHTMLCategory_table,
  86.   eHTMLCategory_tablepart,
  87.   eHTMLCategory_tablerow,
  88.   eHTMLCategory_tabledata,
  89.   eHTMLCategory_head,
  90.   eHTMLCategory_html,
  91.   eHTMLCategory_body,
  92.   eHTMLCategory_form,
  93.   eHTMLCategory_options,
  94.   eHTMLCategory_frameset,
  95.   eHTMLCategory_text
  96. };
  97.  
  98.  
  99. nsresult      ConsumeQuotedString(PRUnichar aChar,nsString& aString,nsScanner& aScanner);
  100. nsresult      ConsumeAttributeText(PRUnichar aChar,nsString& aString,nsScanner& aScanner);
  101. const PRUnichar* GetTagName(PRInt32 aTag);
  102. //PRInt32     FindEntityIndex(nsString& aString,PRInt32 aCount=-1);
  103.  
  104.  
  105.  
  106. /**
  107.  *  This declares the basic token type used in the HTML DTD's.
  108.  *  @update  gess 3/25/98
  109.  */
  110. class CHTMLToken : public CToken {
  111. public:
  112.   virtual ~CHTMLToken();
  113.   CHTMLToken(eHTMLTags aTag);
  114.  
  115.   virtual eContainerInfo GetContainerInfo(void) const {return eFormUnknown;}
  116.   virtual void SetContainerInfo(eContainerInfo aInfo) { }
  117.  
  118. protected:
  119. };
  120.  
  121. /**
  122.  *  This declares start tokens, which always take the form <xxxx>.
  123.  *  This class also knows how to consume related attributes.
  124.  *
  125.  *  @update  gess 3/25/98
  126.  */
  127. class CStartToken: public CHTMLToken {
  128.   CTOKEN_IMPL_SIZEOF
  129.  
  130. public:
  131.   CStartToken(eHTMLTags aTag=eHTMLTag_unknown);
  132.   CStartToken(const nsAString& aString);
  133.   CStartToken(const nsAString& aName,eHTMLTags aTag);
  134.  
  135.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  136.   virtual PRInt32 GetTypeID(void);
  137.   virtual PRInt32 GetTokenType(void);
  138.  
  139.   virtual PRBool IsEmpty(void);
  140.   virtual void SetEmpty(PRBool aValue);
  141.  
  142.   virtual const nsSubstring& GetStringValue();
  143.   virtual void GetSource(nsString& anOutputString);
  144.   virtual void AppendSourceTo(nsAString& anOutputString);
  145.  
  146.   // the following info is used to set well-formedness state on start tags...
  147.   virtual eContainerInfo GetContainerInfo(void) const {return mContainerInfo;}
  148.   virtual void SetContainerInfo(eContainerInfo aContainerInfo) {
  149.     if (eFormUnknown==mContainerInfo) {
  150.       mContainerInfo=aContainerInfo;
  151.     }
  152.   }
  153.   virtual PRBool IsWellFormed(void) const {
  154.     return eWellFormed == mContainerInfo;
  155.   }
  156.  
  157.   nsString mTextValue;
  158. protected:
  159.   eContainerInfo mContainerInfo;
  160.   PRPackedBool mEmpty;
  161. #ifdef DEBUG
  162.   PRPackedBool mAttributed;
  163. #endif
  164. };
  165.  
  166.  
  167. /**
  168.  *  This declares end tokens, which always take the
  169.  *  form </xxxx>. This class also knows how to consume
  170.  *  related attributes.
  171.  *
  172.  *  @update  gess 3/25/98
  173.  */
  174. class CEndToken: public CHTMLToken {
  175.   CTOKEN_IMPL_SIZEOF
  176.  
  177. public:
  178.   CEndToken(eHTMLTags aTag);
  179.   CEndToken(const nsAString& aString);
  180.   CEndToken(const nsAString& aName,eHTMLTags aTag);
  181.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  182.   virtual PRInt32 GetTypeID(void);
  183.   virtual PRInt32 GetTokenType(void);
  184.  
  185.   virtual const nsSubstring& GetStringValue();
  186.   virtual void GetSource(nsString& anOutputString);
  187.   virtual void AppendSourceTo(nsAString& anOutputString);
  188.  
  189. protected:
  190.   nsString mTextValue;
  191. };
  192.  
  193.  
  194. /**
  195.  *  This declares comment tokens. Comments are usually
  196.  *  thought of as tokens, but we treat them that way
  197.  *  here so that the parser can have a consistent view
  198.  *  of all tokens.
  199.  *
  200.  *  @update  gess 3/25/98
  201.  */
  202. class CCommentToken: public CHTMLToken {
  203.   CTOKEN_IMPL_SIZEOF
  204.  
  205. public:
  206.   CCommentToken();
  207.   CCommentToken(const nsAString& aString);
  208.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  209.   virtual PRInt32 GetTokenType(void);
  210.   virtual const nsSubstring& GetStringValue(void);
  211.   virtual void AppendSourceTo(nsAString& anOutputString);
  212.  
  213.   nsresult ConsumeStrictComment(nsScanner& aScanner);
  214.   nsresult ConsumeQuirksComment(nsScanner& aScanner);
  215.  
  216. protected:
  217.   nsScannerSubstring mComment; // does not include MDO & MDC
  218.   nsScannerSubstring mCommentDecl; // includes MDO & MDC
  219. };
  220.  
  221.  
  222. /**
  223.  *  This class declares entity tokens, which always take
  224.  *  the form &xxxx;. This class also offers a few utility
  225.  *  methods that allow you to easily reduce entities.
  226.  *
  227.  *  @update  gess 3/25/98
  228.  */
  229. class CEntityToken : public CHTMLToken {
  230.   CTOKEN_IMPL_SIZEOF
  231.  
  232. public:
  233.   CEntityToken();
  234.   CEntityToken(const nsAString& aString);
  235.   virtual PRInt32 GetTokenType(void);
  236.   PRInt32 TranslateToUnicodeStr(nsString& aString);
  237.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  238.   static nsresult ConsumeEntity(PRUnichar aChar, nsString& aString,
  239.                                 nsScanner& aScanner);
  240.   static PRInt32 TranslateToUnicodeStr(PRInt32 aValue,nsString& aString);
  241.  
  242.   virtual const nsSubstring& GetStringValue(void);
  243.   virtual void GetSource(nsString& anOutputString);
  244.   virtual void AppendSourceTo(nsAString& anOutputString);
  245.  
  246. protected:
  247.   nsString mTextValue;
  248. };
  249.  
  250.  
  251. /**
  252.  *  Whitespace tokens are used where whitespace can be
  253.  *  detected as distinct from text. This allows us to
  254.  *  easily skip leading/trailing whitespace when desired.
  255.  *
  256.  *  @update  gess 3/25/98
  257.  */
  258. class CWhitespaceToken: public CHTMLToken {
  259.   CTOKEN_IMPL_SIZEOF
  260.  
  261. public:
  262.   CWhitespaceToken();
  263.   CWhitespaceToken(const nsAString& aString);
  264.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  265.   virtual PRInt32 GetTokenType(void);
  266.   virtual const nsSubstring& GetStringValue(void);
  267.  
  268. protected:
  269.   nsScannerSharedSubstring mTextValue;
  270. };
  271.  
  272. /**
  273.  *  Text tokens contain the normalized form of html text.
  274.  *  These tokens are guaranteed not to contain entities,
  275.  *  start or end tags, or newlines.
  276.  *
  277.  *  @update  gess 3/25/98
  278.  */
  279. class CTextToken: public CHTMLToken {
  280.   CTOKEN_IMPL_SIZEOF
  281.  
  282. public:
  283.   CTextToken();
  284.   CTextToken(const nsAString& aString);
  285.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  286.   virtual PRInt32 GetTokenType(void);
  287.   virtual PRInt32 GetTextLength(void);
  288.   virtual void CopyTo(nsAString& aStr);
  289.   virtual const nsSubstring& GetStringValue(void);
  290.   virtual void Bind(nsScanner* aScanner, nsScannerIterator& aStart,
  291.                     nsScannerIterator& aEnd);
  292.   virtual void Bind(const nsAString& aStr);
  293.  
  294.   nsresult ConsumeCharacterData(PRBool aConservativeConsume,
  295.                                 PRBool aIgnoreComments,
  296.                                 nsScanner& aScanner,
  297.                                 const nsAString& aEndTagName,
  298.                                 PRInt32 aFlag,
  299.                                 PRBool& aFlushTokens);
  300.  
  301.   nsresult ConsumeParsedCharacterData(PRBool aDiscardFirstNewline,
  302.                                       PRBool aConservativeConsume,
  303.                                       nsScanner& aScanner,
  304.                                       const nsAString& aEndTagName,
  305.                                       PRInt32 aFlag,
  306.                                       PRBool& aFound);
  307.  
  308. protected:
  309.   nsScannerSubstring mTextValue;
  310. };
  311.  
  312.  
  313. /**
  314.  *  CDATASection tokens contain raw unescaped text content delimited by
  315.  *  a ![CDATA[ and ]].
  316.  *  XXX Not really a HTML construct - maybe we need a separation
  317.  *
  318.  *  @update  vidur 11/12/98
  319.  */
  320. class CCDATASectionToken : public CHTMLToken {
  321.   CTOKEN_IMPL_SIZEOF
  322.  
  323. public:
  324.   CCDATASectionToken(eHTMLTags aTag = eHTMLTag_unknown);
  325.   CCDATASectionToken(const nsAString& aString);
  326.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  327.   virtual PRInt32 GetTokenType(void);
  328.   virtual const nsSubstring& GetStringValue(void);
  329.  
  330. protected:
  331.   nsString mTextValue;
  332. };
  333.  
  334.  
  335. /**
  336.  *  Declaration tokens contain raw unescaped text content (not really, but
  337.  *  right now we use this only for view source).
  338.  *  XXX Not really a HTML construct - maybe we need a separation
  339.  *
  340.  */
  341. class CMarkupDeclToken : public CHTMLToken {
  342.   CTOKEN_IMPL_SIZEOF
  343.  
  344. public:
  345.   CMarkupDeclToken();
  346.   CMarkupDeclToken(const nsAString& aString);
  347.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  348.   virtual PRInt32 GetTokenType(void);
  349.   virtual const nsSubstring& GetStringValue(void);
  350.  
  351. protected:
  352.   nsScannerSubstring  mTextValue;
  353. };
  354.  
  355.  
  356. /**
  357.  *  Attribute tokens are used to contain attribute key/value
  358.  *  pairs whereever they may occur. Typically, they should
  359.  *  occur only in start tokens. However, we may expand that
  360.  *  ability when XML tokens become commonplace.
  361.  *
  362.  *  @update  gess 3/25/98
  363.  */
  364. class CAttributeToken: public CHTMLToken {
  365.   CTOKEN_IMPL_SIZEOF
  366.  
  367. public:
  368.   CAttributeToken();
  369.   CAttributeToken(const nsAString& aString);
  370.   CAttributeToken(const nsAString& aKey, const nsAString& aString);
  371.   ~CAttributeToken() {}
  372.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  373.   virtual PRInt32 GetTokenType(void);
  374.   const nsSubstring&     GetKey(void) { return mTextKey.AsString(); }
  375.   virtual void SetKey(const nsAString& aKey);
  376.   virtual void BindKey(nsScanner* aScanner, nsScannerIterator& aStart,
  377.                        nsScannerIterator& aEnd);
  378.   const nsSubstring& GetValue(void) {return mTextValue.str();}
  379.   virtual void SanitizeKey();
  380.   virtual const nsSubstring& GetStringValue(void);
  381.   virtual void GetSource(nsString& anOutputString);
  382.   virtual void AppendSourceTo(nsAString& anOutputString);
  383.  
  384.   PRPackedBool mHasEqualWithoutValue;
  385. protected:
  386. #ifdef DEBUG
  387.   PRPackedBool mLastAttribute;
  388. #endif
  389.   nsScannerSharedSubstring mTextValue;
  390.   nsScannerSubstring mTextKey;
  391. };
  392.  
  393.  
  394. /**
  395.  *  Newline tokens contain, you guessed it, newlines.
  396.  *  They consume newline (CR/LF) either alone or in pairs.
  397.  *
  398.  *  @update  gess 3/25/98
  399.  */
  400. class CNewlineToken: public CHTMLToken {
  401.   CTOKEN_IMPL_SIZEOF
  402.  
  403. public:
  404.   CNewlineToken();
  405.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  406.   virtual PRInt32 GetTokenType(void);
  407.   virtual const nsSubstring& GetStringValue(void);
  408.  
  409.   static void AllocNewline();
  410.   static void FreeNewline();
  411. };
  412.  
  413.  
  414. /**
  415.  *  Whitespace tokens are used where whitespace can be
  416.  *  detected as distinct from text. This allows us to
  417.  *  easily skip leading/trailing whitespace when desired.
  418.  *
  419.  *  @update  gess 3/25/98
  420.  */
  421. class CInstructionToken: public CHTMLToken {
  422.   CTOKEN_IMPL_SIZEOF
  423.  
  424. public:
  425.   CInstructionToken();
  426.   CInstructionToken(const nsAString& aString);
  427.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  428.   virtual PRInt32 GetTokenType(void);
  429.   virtual const nsSubstring& GetStringValue(void);
  430.  
  431. protected:
  432.   nsString mTextValue;
  433. };
  434.  
  435.  
  436. /**
  437.  * This token is generated by the HTML and Expat tokenizers
  438.  * when they see the doctype declaration ("<!DOCTYPE ... >")
  439.  *
  440.  */
  441.  
  442. class CDoctypeDeclToken: public CHTMLToken {
  443.   CTOKEN_IMPL_SIZEOF
  444.  
  445. public:
  446.   CDoctypeDeclToken(eHTMLTags aTag=eHTMLTag_unknown);
  447.   CDoctypeDeclToken(const nsAString& aString,eHTMLTags aTag=eHTMLTag_unknown);
  448.   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  449.   virtual PRInt32 GetTokenType(void);
  450.   virtual const nsSubstring& GetStringValue(void);
  451.   virtual void SetStringValue(const nsAString& aStr);
  452.  
  453. protected:
  454.   nsString mTextValue;
  455. };
  456.  
  457. #endif
  458.